package com.cloud.pole.shop.biz.shops;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ace.cache.annotation.Cache;
import com.ace.cache.annotation.CacheClear;
import com.cloud.pole.common.biz.BaseBiz;
import com.cloud.pole.mapper.shops.ShopShopsMapper;
import com.cloud.pole.shop.model.entity.shops.ShopShops;
import com.cloud.pole.shop.model.entity.shops.ShopShopsAdmin;
import com.cloud.pole.shop.model.entity.shops.ShopShopsWithBLOBs;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

/**
 * ${DESCRIPTION}
 *
 * @author wanghaobin
 * @create 2017-06-08 16:23
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ShopShopsBiz extends BaseBiz<ShopShopsMapper, ShopShopsWithBLOBs> {

	@Cache(key = "shopShops{1}")
	public ShopShopsWithBLOBs selectById(Integer id) {
		return super.selectById(id);
	}

	@Cache(key = "shopShopsList{1}-merchantId")
	public List<ShopShopsWithBLOBs> selectListByMerchantId(Integer merchantId) {
		Example example = new Example(ShopShopsWithBLOBs.class);
		Criteria criteria = example.createCriteria();
		example.setOrderByClause("shop_id desc");
		criteria.andEqualTo("merchantId", merchantId);
		return mapper.selectByExample(example);
	}

	@CacheClear(key = "shopShopsList{1.merchantId}-merchantId")
	public void insertUseGeneratedKeys(ShopShopsWithBLOBs entity) {
		mapper.insertUseGeneratedKeys(entity);
	}

	@Override
	@CacheClear(keys = { "shopShopsList{1.merchantId}-merchantId", "shopShops{1.shopId}" })
	public void updateSelectiveById(ShopShopsWithBLOBs entity) {
		super.updateSelectiveById(entity);
	}

	/**
	 * 调用存储过程给店铺添加初始化数据
	 * 
	 * @param map
	 * @return
	 */
	public Map<String, Object> addDefaultData(Map<String, Object> map) {
		return mapper.addDefaultData(map);
	}

	public List<ShopShops> selectListByManageUserId(Long userId, Integer shopId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userId", userId);
		map.put("shopId", shopId);
		return mapper.selectListByManageUserId(map);
	}

	public List<ShopShopsWithBLOBs> selectPageShopAdminList(String shopName) {
		Example example = new Example(ShopShopsWithBLOBs.class);
		Criteria criteria = example.createCriteria();
		if (StringUtils.isNoneBlank(shopName)) {
			criteria.andLike("shopName", "%" + shopName + "%");
		}
		return mapper.selectByExample(example);
	}
	
	public void appendShopShops(ShopShops shopShops) {
		mapper.appendShopShops(shopShops);
	}

	@CacheClear(key="shopShops{1.shopId}")
	public void updatendShopShops(ShopShops shopShops) {
		mapper.updateShopShops(shopShops);
	}
}
